/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.GestionnaireDamier;
import Modele.Grille;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author david
 */
public class FenetreJeuDeDames extends JFrame implements MouseListener, ComponentListener{
    
    Grille modele;
    GestionnaireDamier gestionnaire;
    DamierPanel dp;
    
    boolean choix_case = false; // True = choix de la case où doit se déplacer le pion, false = choix du pion à déplacer
    
    public FenetreJeuDeDames(Grille m,GestionnaireDamier g){
        this.setTitle("Jeu de dames");
        this.setSize(400 , 400);
        this.setLocationRelativeTo(null);               
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dp = new DamierPanel(m);
        this.setContentPane(dp);
        this.setVisible(true);
        this.modele = m;
        this.gestionnaire = g;
        addMouseListener(this);
        addComponentListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        
    }

    @Override
    public void mousePressed(MouseEvent e) {       
        int[] pos = dp.getCase(e.getX(), e.getY());        
        if(!choix_case){            
            if(gestionnaire.choixPion(pos[0],pos[1]))
                choix_case = true;  
        }else{            
            gestionnaire.choixCase(pos[0],pos[1]);
            gestionnaire.mouvement();
            choix_case = false; // On retourne au choix du pion même si sa n'a pas fonctionné            
        }
        
        if(modele.blancGagne()){            
            JOptionPane.showMessageDialog(null, "Les pions blancs gagnent", "Victoire", JOptionPane.INFORMATION_MESSAGE);            
            this.dispose();
        }else if(modele.noirGagne()){
            JOptionPane.showMessageDialog(null, "Les pions noirs gagnent", "Victoire", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void componentResized(ComponentEvent e) {
        if(this.getWidth() != this.getHeight())
        {
            int taille;
            if(this.getWidth() < this.getHeight() )
                taille = this.getWidth() / 100;
            else
                taille = this.getHeight() / 100;
            taille *= 100;
            this.setSize(taille+16,taille+38);
        }
    }

    @Override
    public void componentMoved(ComponentEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void componentShown(ComponentEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void componentHidden(ComponentEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
